package com.lp.controller;

import com.lp.po.SceneryApplets;
import com.lp.service.ArgumentService;
import com.lp.service.SceneryAppletsService;
import com.lp.utils.ImageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin/Applets")
public class AdminAppletsController {
    @Resource
    private SceneryAppletsService sceneryAppletsService;
    @Resource
    private ArgumentService argumentService;

    // 前端小程序管理
    @RequestMapping(value = "/showApplets")
    public ModelAndView showApplets() throws Exception {
        ModelAndView mv=new ModelAndView();
        SceneryApplets list=sceneryAppletsService.findAllSceneryApplets(1);
        mv.addObject("applets",list);
        mv.setViewName("admin/showApplets");
        return mv;
    }
    @RequestMapping(value = "/editSceneryApplets",method = RequestMethod.GET)
    public String editScenery(Model model) throws Exception {
        SceneryApplets list=sceneryAppletsService.findAllSceneryApplets(1);
        model.addAttribute("applets", list);
        return "/admin/editSceneryApplets";
    }

    @RequestMapping(value = "/doEditSceneryApplets",method = RequestMethod.POST)
    public String doEditScenery(@RequestParam("file")MultipartFile file, @RequestParam("videofile") CommonsMultipartFile videofile,
                                SceneryApplets sceneryApplets) throws Exception {
        String uploadUrl = argumentService.getByName("uploadUrl").getValue();

        //上传背景图片
        if(!file.isEmpty()){
            sceneryApplets.setBackground(ImageUtil.getNewFileName(file,uploadUrl));
        }
        // 上传视频
        if (!videofile.isEmpty()) {
            sceneryApplets.setVideosrc(ImageUtil.getNewFileName(videofile,uploadUrl));
        }
        //更新数据并设置评论功能权限
        sceneryAppletsService.updata(sceneryApplets);
        return "redirect:/admin/Applets/showApplets";
    }


}
